package com.software.questioncenter.service;

import com.software.questioncenter.entity.Answer;
import com.software.questioncenter.exception.BizException;
import com.software.questioncenter.repository.AnswerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * Author: wjx
 * Description: 题解Answer的服务层实现类
 */
@Service
public class AnswerServiceImpl implements AnswerService {

    @Autowired
    private AnswerRepository answerRepository;

    //新增题解
    public void createAnswer(Answer answer) {
        //查询题目是否在题库中存在
        Optional<Answer> optional = answerRepository.findById(answer.getId());
        //如果存在
        if (optional.isPresent()){
            throw new BizException("该题目已经在题库中存在");
        }
        answerRepository.save(answer);
    }


    //查看题解列表
    @Override
    public Page queryAnswerList(String questionId, Pageable pageable) {
        Specification<Answer> specification = new Specification<Answer>() {
            @Override
            public Predicate toPredicate(Root<Answer> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(!StringUtils.isEmpty(questionId)){
                    predicates.add(criteriaBuilder.equal(root.get("questionId"), questionId));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        Page page = answerRepository.findAll(specification, pageable);
        return page;
    }

    //查看具体题解
    @Override
    public Answer findById(String answerId) {
        Optional<Answer> optional = answerRepository.findById(answerId);
        if (!optional.isPresent()){
            throw new BizException("未查询到需要查看的题目，请核对题目id是否正确");
        }
        return optional.get();
    }

    @Override
    public List<Answer> queryAnswerByContent(String content, Integer questionId) {
        Specification<Answer> specification = new Specification<Answer>() {
            @Override
            public Predicate toPredicate(Root<Answer> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(!StringUtils.isEmpty(content)){
                    predicates.add(criteriaBuilder.like(root.get("content"), "%" + content + "%"));
                }
                if (!StringUtils.isEmpty(questionId)){
                    predicates.add(criteriaBuilder.equal(root.get("questionId"), questionId));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        List all = answerRepository.findAll(specification);
        return all;
    }


}
